package org.dtrd.modules.archive.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dtrd.config.system.Constant;
import org.dtrd.modules.archive.entity.bean.DtrdEntRdOverviewDTO;
import org.dtrd.modules.archive.entity.bean.report.DiabetesOverviewReport;
import org.dtrd.modules.archive.entity.po.DtrdEntRdArchiveOverview;
import org.dtrd.modules.archive.mapper.DtrdEntRdOverviewMapper;
import org.dtrd.modules.archive.service.IDtrdEntRdOverviewService;
import org.dtrd.modules.patient.entity.po.DtrdEntRdPatient;
import org.dtrd.modules.patient.service.IDtrdEntRdPatientService;
import org.dtrd.modules.system.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.StringJoiner;

/**
 * @author 萧
 * @date 2021-10-01 22:47:27
 **/
@Service
@DS("multi-datasource1")
public class DtrdEntRdOverviewServiceImpl extends ServiceImpl<DtrdEntRdOverviewMapper, DtrdEntRdArchiveOverview> implements IDtrdEntRdOverviewService {

    @Autowired
    private ISysDictService dictService;

    @Autowired
    private IDtrdEntRdPatientService patientService;

    private final static String symptoms = "symptoms";

    @Override
    public DiabetesOverviewReport getPatientDiabetesOverviewReport(Integer patientId) {
        DtrdEntRdArchiveOverview po = getPatientDiabetesOverview(patientId);
        DiabetesOverviewReport report = null;
        if (po != null) {
            report = new DiabetesOverviewReport().parseFromPO(po);
            if (po.getIfDiabetes() != null) {
                report.setIfDiabetesText(Constant.DiagnoseStatus.valueOf(po.getIfDiabetes()).getName());
            }
            // 起病类型
            String diabetesOnsetSymptom = po.getDiabetesOnsetSymptom();
            if (StrUtil.isNotBlank(diabetesOnsetSymptom)) {
                StringJoiner joiner = new StringJoiner("、");
                String[] strings = diabetesOnsetSymptom.split(",");
                for (String s : strings) {
                    String text = dictService.queryDictTextByKey(symptoms, s);
                    joiner.add(text);
                }
                if (StrUtil.isNotBlank(po.getDiabetesOnsetSymptomOther())) {
                    joiner.add(po.getDiabetesOnsetSymptomOther());
                }
                report.setDiabetesOnsetSymptom(joiner.toString());
            }
        }
        return report;
    }

    @Override
    public DtrdEntRdArchiveOverview getPatientDiabetesOverview(Integer patientId) {
        return getOne(Wrappers.lambdaQuery(DtrdEntRdArchiveOverview.class)
                .eq(DtrdEntRdArchiveOverview::getPatientId, patientId)
                .eq(DtrdEntRdArchiveOverview::getIsDel, 0)
                .last(" limit 1 ")
        );
    }

    @Override
    public boolean saveOrUpdateByPatientId(DtrdEntRdArchiveOverview po, Integer patientId) {
        LambdaUpdateWrapper<DtrdEntRdArchiveOverview> wrapper = Wrappers.lambdaUpdate(DtrdEntRdArchiveOverview.class)
                .eq(DtrdEntRdArchiveOverview::getPatientId, patientId)
                .eq(DtrdEntRdArchiveOverview::getIsDel, 0);
        return saveOrUpdate(po, wrapper);
    }

    @Override
    public void updatePatientDiabetesType(Integer patientId, Integer diabetesType) {
        DtrdEntRdArchiveOverview overview = getPatientDiabetesOverview(patientId);
        if (overview == null) {
            overview = new DtrdEntRdArchiveOverview().init();
        }
        overview.setDiabetesType(diabetesType);
        overview.setIfDiabetes(Constant.DiagnoseStatus.HAD_DIAGNOSED.getType());
        LambdaUpdateWrapper<DtrdEntRdArchiveOverview> updateWrapper = Wrappers.lambdaUpdate(DtrdEntRdArchiveOverview.class)
                .eq(DtrdEntRdArchiveOverview::getPatientId, patientId);
        update(overview, updateWrapper);
    }

    @Override
    public List<DtrdEntRdArchiveOverview> listOverviewByPatientIds(List<Integer> patientIds) {
        LambdaQueryWrapper<DtrdEntRdArchiveOverview> wrapper = Wrappers.lambdaQuery(DtrdEntRdArchiveOverview.class)
                // 只查询已确认的记录
                .eq(DtrdEntRdArchiveOverview::getIfDiabetes, Constant.DiagnoseStatus.HAD_DIAGNOSED.getType())
                .in(DtrdEntRdArchiveOverview::getPatientId, patientIds);
        return list(wrapper);
    }

    @Override
    public boolean saveOrUpdateRecord(DtrdEntRdOverviewDTO overviewDTO) {
        Integer dataId = overviewDTO.getDataId();
        DtrdEntRdArchiveOverview overview = getPatientDiabetesOverview(overviewDTO.getPatientId());
        if (overview != null) {
            // 编辑
            if (!Objects.equals(overview.getDataId(), dataId)) {
                // 记录ID不一致 参数错误
                return false;
            }
        } else {
            // 新增
            overview = new DtrdEntRdArchiveOverview().init();
            overviewDTO.setDataId(null);
        }
        BeanCopier beanCopier = BeanCopier.create(DtrdEntRdOverviewDTO.class, DtrdEntRdArchiveOverview.class, false);
        beanCopier.copy(overviewDTO, overview, null);
        boolean result = saveOrUpdate(overview);
        if (result) {
            // 更新关联的数据
            Integer diabetesType = overviewDTO.getDiabetesType();
            if (diabetesType != null) {
                DtrdEntRdPatient patient = patientService.getById(overviewDTO.getPatientId());
                if (patient != null) {
                    patient.setPatientsDiabetisType(diabetesType);
                    patient.setPatientsConfirmedDate(overviewDTO.getDiabetesDiagnoseDate());
                    patientService.updateById(patient);
                }
            }
        }
        return result;
    }

}
